// Author: Mark Richardson
// Date: 01/28/2022
// Multi-rater item response model for latent traits with informed prior on the latent traits
// Use standard deviation of the informed ratings for the prior
// Proofed 11/09/2023

data {
  
  int<lower = 0> N; // number of ratings
  
  int<lower = 0> A; // number of agencies
  
  int<lower = 0> R; // number of raters
  
  int<lower = 1, upper = A> aa[N]; // agency index
  
  int<lower = 1, upper = R> rr[N]; // rater index
  
  vector[N] y; // vector of ratings
  
  vector[A] mu_prior; // vector of informed means
  
  vector[A] sd_prior; // vector of informed standard deviations
  
}


parameters {
  
  real mu_alpha; // grand mean for rater intercepts
  
  vector[R] alpha; // rater intercept parameters
  
  vector<lower = 0>[R] beta; // rater slope parameters

  vector[A] x; // latent performance

  real<lower = 0> sigma; // scale of latent performance
  
  real<lower = 0> sigma_alpha; // scale of rater intercepts
  
  real<lower = 0> sigma_beta; // scale of rater slopes
  
}


model {
  
  alpha ~ normal(0, sigma_alpha);
  
  beta ~ normal(0, sigma_beta);
  
  x ~ normal(mu_prior, sd_prior); // Informed prior on latent trait
  
  mu_alpha ~ normal(0, 5);
  
  sigma ~ normal(0, 5);
  
  sigma_alpha ~ normal(0, 5);
    
  sigma_beta ~ normal(0, 5);
  
  for (n in 1:N) {
    
    y[n] ~ normal((mu_alpha + alpha[rr[n]]) + beta[rr[n]] * x[aa[n]], sigma);

  }
}

